package com.mxpio.erp.plm.controller;

import com.mxpio.erp.common.plm.entity.ProjectDeliverable;
import com.mxpio.erp.common.plm.entity.ProjectTaskDeliverableTemplate;
import com.mxpio.erp.common.plm.service.ProjectTaskDeliverableTemplateService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ProjectTaskDeliverableTemplateController", description = "项目-任务-交付物模板")
@RestController
@RequestMapping("/erp/plm/pro/task/deliverable/template/")
public class ProjectTaskDeliverableTemplateController {

    @Autowired
    private ProjectTaskDeliverableTemplateService projectTaskDeliverableTemplateService;

    @GetMapping("list")
    @Operation(summary = "任务交付物模板列表", description = "任务交付物模板列表（无分页）", method = "GET")
    public Result<List<ProjectTaskDeliverableTemplate>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<ProjectTaskDeliverableTemplate> list = projectTaskDeliverableTemplateService.list(ProjectTaskDeliverableTemplate.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "任务交付物模板列表", description = "任务交付物模板列表（分页）", method = "GET")
    public Result<Page<ProjectTaskDeliverableTemplate>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProjectTaskDeliverableTemplate> pageResult = projectTaskDeliverableTemplateService.listPage(ProjectTaskDeliverableTemplate.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{docNO}")
    @Operation(summary = "根据任务交付物模板编码-获取交付物信息", description = "根据任务交付物模板编码-获取交付物信息", method = "GET")
    public Result<ProjectTaskDeliverableTemplate> getById(@PathVariable(name = "docNO", required = true) String docNO) {
        ProjectTaskDeliverableTemplate deliverables = projectTaskDeliverableTemplateService.getById(ProjectTaskDeliverableTemplate.class, docNO);
        return Result.OK(deliverables);
    }

    @RequestMapping("add")
    @Operation(summary = "保存任务交付物模板", description = "保存任务交付物模板")
    public Result<ProjectTaskDeliverableTemplate> add(@RequestBody ProjectTaskDeliverableTemplate deliverables) {
        projectTaskDeliverableTemplateService.save(deliverables);
        return Result.OK(deliverables);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑任务交付物模板", description = "编辑任务交付物模板", method = "PUT")
    public Result<ProjectTaskDeliverableTemplate> edit(@RequestBody ProjectTaskDeliverableTemplate deliverables) {
        projectTaskDeliverableTemplateService.update(deliverables);
        return Result.OK(deliverables);
    }


    @DeleteMapping("delete/{docNO}")
    @Operation(summary = "通过docNO删除", description = "通过docNO删除", method = "DELETE")
    public Result<ProjectTaskDeliverableTemplate> delete(@PathVariable(name="docNO",required=true) String docNO) {
        projectTaskDeliverableTemplateService.delete( ProjectTaskDeliverableTemplate.class,docNO);
        return Result.OK();
    }

    @GetMapping("list/task/{taskCode}")
    @Operation(summary = "根据任务编码-获取任务交付物模板", description = "根据任务编码-获取任务交付物模板", method = "GET")
    public Result<List<ProjectTaskDeliverableTemplate>> getByList(@PathVariable(name = "taskCode", required = true) String taskCode) {
        Criteria criteria = Criteria.create().addCriterion("taskCode", Operator.EQ, taskCode);
        List<ProjectTaskDeliverableTemplate> list = projectTaskDeliverableTemplateService.list(ProjectTaskDeliverableTemplate.class, criteria);
        return Result.OK(list);
    }
}
